package DAO;


import Entity.Account;
import Entity.ICcard;
import util.Get_Now_Time;
import util.Result;
import util.RfidTool;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AccountDao {
    private static short tsector = 1;
    private static short tblk = 6;

    static Connection connect = null;//数据库连接对象
    static PreparedStatement cmmd = null;//数据库命令执行对象
    static ResultSet rs = null;//数据集
    static String sql = "";


    RfidTool instance = RfidTool.getInstance();
    String cardID = instance.showCardId();//获取id卡的号码
    Get_Now_Time getNowTime = new Get_Now_Time();

    public String msg() {
        String msg = instance.getdeviceInti();
        System.out.println(msg);
        return msg;
    }

    public Result insert(Account in) {
        Result result = new Result();
        sql = "insert into Account values(?,?,?,?,?,?,?)";
        try {

            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            in.setTime(getNowTime.Getnow());

            cmmd.setString(1, in.getIcID());
            cmmd.setBoolean(2, in.isOpration());//传入参数的个数
            cmmd.setString(3, in.getTime());
            cmmd.setString(4, in.getMoney());
            cmmd.setString(5, in.getScNo());
            cmmd.setString(6, in.getACNo());
            cmmd.setString(7, in.getRest());
            int i = cmmd.executeUpdate();
            System.out.println(i);

            if (in.getIcID() != null && in.isOpration() != false && in.getTime() != null && in.getScNo() != null && in.getACNo() != null) {//判断文本框输入是否为空
                JOptionPane.showMessageDialog(null, "添加成功！");
            }
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }


    public Result update3(Account in) {
        Result result = new Result();
        in.setTime(getNowTime.Getnow());
        sql = "update Account set Operation = '" + in.isOpration() + "',Money= '" + in.getMoney() + "',scNo= '" + in.getScNo() + "',Rest= '" + in.getRest() + "', Time ='" + in.getTime() + "' where IcID = '" + in.getIcID() + "',ACNo ='" + in.getACNo() + "',";
        try {

            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            cmmd.execute();


        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }

    public Result Delete(String out) {

        Result result = new Result();
        sql = "delete from Account where ACNo='"+out+"'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            cmmd.execute(); //执行sql语句
            cmmd.executeUpdate();
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }

    public Result Querry(String x) {

        Result result = new Result();
        List<Account> list = new ArrayList<>();
        Account count;


        sql = "Select * from Account where ACNo like '%" + x + "%' order by ACNo+''";

        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            cmmd.setString(1, x);
            System.out.println(cmmd);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                count = new Account();
                count.setIcID(rs.getString(1));
                count.setOpration(rs.getBoolean(2));
                count.setTime(rs.getString(3));
                count.setMoney(rs.getString(4));
                count.setScNo(rs.getString(5));
                count.setACNo(rs.getString(6));
                count.setRest(rs.getString(7));
                list.add(count);
            }
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        result.setMessage(list);
        return result;


    }
}
